package com.obrempon.credit.market.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import android.os.Build
import android.os.CountDownTimer
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.text.style.TypefaceSpan
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.RequiresApi
import com.obrempon.credit.market.R
import com.obrempon.credit.market.common.CommonDialog
import com.obrempon.credit.market.databinding.ObremponUserDataClearDialogBinding
import com.obrempon.credit.market.event.ObremponEventPointType

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/27 15:50
 * @Annotation：清空用户数据dialog
 */
class ObremponUserDataClearDialog: CommonDialog {

    private lateinit var binding: ObremponUserDataClearDialogBinding
    private var mCallback: IDialogCallback? = null

    //等待时长
    private var countdownTimer: MyCountDownTimer? = null
    private var countdown = 10

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun takeObremponRootView(): View {
        binding = ObremponUserDataClearDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initObremponView() {

    }

    override fun initObremponListener() {
        setOnClickListener(binding.buttonReturn)
        setOnClickListener(binding.buttonConfirm)
    }

    override fun takeObremponData() {

    }

    override fun onObremponViewClick(v: View?) {
        dismiss()
        when(v!!.id){
            R.id.buttonConfirm -> {
                if(mCallback != null){
                    mCallback!!.onUserDataClearConfirmClick()
                }
            }
        }
    }

    fun showObremponDeletePersonalDataDesc(desc: String, date: String){
        val showDesc = "$desc $date."
        val descSpannable = SpannableString(showDesc)
        val startIndex = showDesc.indexOf(date)
        descSpannable.setSpan(ForegroundColorSpan(getColor(R.color.color_ffff0000)), startIndex, startIndex + date.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        descSpannable.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + date.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        descSpannable.setSpan(RelativeSizeSpan(1.1f),startIndex,startIndex+date.length,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textDeleteDataDesc.text = descSpannable
    }

    interface IDialogCallback {
        //按钮点击
        fun onUserDataClearConfirmClick()
    }

    class MyCountDownTimer(private val dialog: ObremponUserDataClearDialog, millisInFuture: Int, countDownInterval: Long): CountDownTimer(millisInFuture.toLong(), countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            dialog.countdown = (millisUntilFinished / 1000).toInt()
            dialog.checkClearUserDataConfirmStatus()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            dialog.checkClearUserDataConfirmStatus()
        }
    }

    /**
     * 启动倒计时
     */
    @SuppressLint("SetTextI18n")
    private fun startCountdownTimer() {
        //随机 60秒 等待时长
        countdown = 10
        checkClearUserDataConfirmStatus()
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this, countdown * 1000, 1000)
        }
        countdownTimer?.start()
    }

    @SuppressLint("SetTextI18n")
    private fun checkClearUserDataConfirmStatus(){
        if (countdown <= 0){
            binding.buttonConfirm.isEnabled = true
            binding.buttonConfirm.text = getString(R.string.obrempon_button_confirm)
        } else {
            binding.buttonConfirm.isEnabled = false
            binding.buttonConfirm.text = "${getString(R.string.obrempon_button_confirm)}(${countdown}s)"
        }
    }

    override fun onStart() {
        super.onStart()
        startCountdownTimer()
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_USER_DATA_CLEAR_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        if (countdownTimer != null) {
            countdownTimer!!.cancel()
            countdownTimer = null
        }
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_USER_DATA_CLEAR_DISMISS)
    }
}